package org.kl.bf.service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.BusinessEntity;
import org.kl.bf.repository.BasicDao;
import org.kl.bf.utils.BasicConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;

/**
 * @author FDQ
 * @date 2015-9-1
 */
@Component
public abstract class BasicService<T extends Serializable> {

	@Autowired
	protected BasicDao<T> basicDao;
	protected Class<T> entityClazz;

	public T findOneAndClear(String id) {
		T result = basicDao.findOne(id);
		basicDao.getEntityManager().clear();
		return result;
	}

	public T getOne(String id) {
		return basicDao.findOne(id);
	}

	public void save(T entity) {
		basicDao.save(entity);
	}

	public void save(T entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		basicDao.save(entity);
		List<T> inserts = JSON.parseArray(detailInserted, entityClazz);
		if (Collections3.isNotEmpty(inserts)) {
			for (T insert : inserts) {
				// 增加与主表相关的数据复制代码
				basicDao.save(insert);
			}
		}
		List<T> updates = JSON.parseArray(detailUpdated, entityClazz);
		if (updates.size() > 0) {
			for (T update : updates) {
				basicDao.save(update);
			}
		}
		List<T> deletes = JSON.parseArray(detailDeleted, entityClazz);
		if (Collections3.isNotEmpty(deletes)) {
			basicDao.delete(deletes);
		}
	}

	public void delete(String id) {
		basicDao.delete(id);
	}

	public List<T> getAll() {
		return (List<T>) basicDao.findAll();
	}

	public List<T> getList(List<String> ids) {
		return (List<T>) basicDao.findAll(ids);
	}

	public List<T> getList(String[] ids) {
		return (List<T>) basicDao.findAll(Arrays.asList(ids));
	}

	/**
	 * 按条件查找全部
	 * 
	 * @param searchParams
	 * @return
	 */
	public List<T> searchAll(Map<String, Object> searchParams) {
		return basicDao.searchAll(searchParams, entityClazz);
	}

	/**
	 * 按条件查询全部，带排序
	 * 
	 * @return
	 */
	public List<T> searchAll(Map<String, Object> searchParams, String sortType, String orderType) {
		return basicDao.searchAll(searchParams, sortType, orderType, entityClazz);
	}

	/**
	 * 分页查询
	 */
	public Page<T> getPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType, String orderType) {
		return basicDao.getPage(searchParams, pageNumber, pageSize, sortType, orderType, entityClazz);
	}

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				T entity = basicDao.findOne(id);
				if (entity instanceof BusinessEntity) {
					BusinessEntity one = (BusinessEntity) entity;
					if (!one.getConfirmFlag().equals(BasicConstants.CONFIRM_STATUS_Draft)) {
						continue;
					}
				}
				basicDao.delete(id);
				i++;
			}
		}
		return "成功删除" + i + "条记录";
	}

}
